package vashion.azeroth.api.controller.item;

import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;

import vashion.azeroth.api.controller.base.BaseController;
import vashion.azeroth.core.item.pojo.ItemBrandDO;
import vashion.azeroth.core.item.pojo.ItemYvogueBrandDO;
import vashion.azeroth.core.manager.item.BaseItemBrandManager;
import vashion.azeroth.core.manager.item.ItemYvogueBrandManager;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.yvogue.pojo.YvogueBrand;
import vashion.azeroth.core.yvogue.pojo.YvogueBrandList;
import vashion.azeroth.core.yvogue.sftp.SFTPUtil;
import vashion.azeroth.core.yvogue.xml.XMLUtil;

@Controller
@RequestMapping("/api/brand")
public class ItemBrandController<T> extends BaseController {

	private final static Logger log = LoggerFactory.getLogger(ItemBrandController.class);

	@Autowired
	private BaseItemBrandManager baseItemBrandManager;

	@Autowired
	private ItemYvogueBrandManager itemYvogueBrandManager;

	/**
	 * 获取完整品牌列表
	 *
	 * @return
	 */
	@RequestMapping("getBrandList.htm")
	@ResponseBody
	public ApiResult<List<ItemBrandDO>> getBrandList(@ModelAttribute("itemBrandDO") ItemBrandDO itemBrandDO) {
		return baseItemBrandManager.getItemBrandList(itemBrandDO);
	}

	/**
	 * 映射yvogue完整品牌列表
	 *
	 * @return
	 */
	@RequestMapping("getYvogueBrandList.htm")
	@ResponseBody
	public ApiResult<List<ItemBrandDO>> getYvogueBrandList() {
		SFTPUtil sftp = new SFTPUtil("inventory", "Ihxy&L", "121.40.188.248", 22);
		try {
			sftp.login();

			String xmlStr = sftp.download("./", "brands.xml");
			sftp.logout();
			YvogueBrandList yvogueBrandList = (YvogueBrandList) XMLUtil.convertXmlStrToObject(YvogueBrandList.class,
					xmlStr);
			List<String> normalBrand = Lists.<String>newArrayList();
			List<String> emptyBrand = Lists.<String>newArrayList();
			List<String> multilBrand = Lists.<String>newArrayList();
			Iterator<YvogueBrand> it = yvogueBrandList.iterator();
			while (it.hasNext()) {
				YvogueBrand yvogueBrandTemp = (YvogueBrand) it.next();
				ItemYvogueBrandDO itemYvogueBrandDO = new ItemYvogueBrandDO();
				itemYvogueBrandDO.setYvogueBrandId(yvogueBrandTemp.getId());
				itemYvogueBrandDO.setYvogueBrandTitle(yvogueBrandTemp.getName());
				itemYvogueBrandDO.setUserId(0L);
				ItemBrandDO itemBrandDO = new ItemBrandDO();
				itemBrandDO.setTitle(yvogueBrandTemp.getName());
				List<ItemBrandDO> itemBrandList = baseItemBrandManager.find(itemBrandDO);

				if (null == itemBrandList || itemBrandList.isEmpty()) {
					emptyBrand.add(yvogueBrandTemp.getName());
				} else {
					if (itemBrandList.size() == 1) {
						normalBrand.add(yvogueBrandTemp.getName());
						itemBrandDO = itemBrandList.get(0);
						itemYvogueBrandDO.setBrandId(itemBrandDO.getId());
						itemYvogueBrandDO.setBrandTitle(itemBrandDO.getTitle());
						itemYvogueBrandDO.setDescription(itemBrandDO.getDescription());
					} else {
						multilBrand.add(yvogueBrandTemp.getName());
						itemYvogueBrandDO.setBrandId(1000L);
					}
				}
				itemYvogueBrandManager.publishYvogueBrand(itemYvogueBrandDO);
			}

			for (String brandName : normalBrand) {
				System.out.println("有一个对应的品牌:" + brandName);
			}
			for (String brandName : emptyBrand) {
				System.out.println("无对应的品牌:" + brandName);
			}
			for (String brandName : multilBrand) {
				System.out.println("有多个对应的品牌:" + brandName);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
